package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.dto.LoginRequest;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import com.itheima.utils.Result;
import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @PostMapping
    public Result<UserInfoVo> login(@RequestBody LoginRequest request) {
        UserInfoVo userInfoVo = loginService.login(request);

        Map<String, Object> claims = BeanUtil.beanToMap(userInfoVo);

        String jwt = JwtUtils.generateJwt(claims);
        userInfoVo.setToken(jwt);


        return Result.success(userInfoVo);
    }
}
